// pages/myidentityList/myidentityList.js
//logs.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
var timeOut = 60;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:applet_park_api_url_imgUrl,
    authCode: '',
    mobile: '',
    showModal: false,
    tipsCode: "获取验证码",
    timeNum: 60,
    clikType: false,
    currentTab: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var role = []
    var list = {
      roleName: "身份介绍",
      elishRoleName: "introduction",
      icon: '',
      bgstart: '#ff9f3c',
      bgend: '#ff9f3c',
      bg: 1,
    }

    role.push(list);
    var list = {
      roleName: "已备案",
      elishRoleName: "Park administrator ",
      icon: '',
      bgstart: '#2666FC',
      bgend: '#54B0F1',
      bg: 2,
    }
    role.push(list);
    var list = {
      roleName: "成为租户",
      elishRoleName: "Tenant",
      icon: '',
      bgstart: '#2666FC',
      bgend: '#54B0F1',
      bg: 2,
    }
    role.push(list);
    var list = {
      roleName: "成为员工",
      elishRoleName: "Owner",
      icon: '',
      bgstart: '#ff9f3c',
      bgend: '#ff9f3c',
      bg: 1,
    }
    role.push(list);

    this.data.role = role;
    // this.(this.data);
    this.setData({
      role: role
    })
  },
  chioce(e) {

    var index = e.currentTarget.dataset.index;

    var chiceid = e.currentTarget.dataset.id;

    this.setData({
      selected: index,
      chiceid: chiceid
    });

    switch (index) {
      case 0:

        wx.navigateTo({
          url: '../Identityintroduction/Identityintroduction',
        })
        break;
      case 1:
        this.openzp();
        break;
      case 2:
        wx.navigateTo({
          url: '../tenantCertification/tenantCertification',
        })
        break;
      case 3:
        wx.navigateTo({
          url: '../Employee_certification/Employee_certification',
        })
        break;
    }
  },
  go: function() {
    this.setData({
      showModal: false
    })
  },

  calsubmit() {
    var that = this
    let url = '';

    if (!that.data.calcontent) {
      util.showModal('请填写退回备注');
      return
    }
    var id = wx.getStorageSync("id");

    
    if (that.data.model == 'm1') {
      url = applet_park_api_url + '/api/repairs/return'

    }
    if (that.data.model == 'm2') {
      url = applet_park_api_url + '/api/repairs/cancel'

    }

    wx.request({
      url: url,
      data: {
        repairId: that.data.workid,
        assignorId: id,
        reason: that.data.calcontent
      },
      method: 'PUT',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      success: function(res) {
        
        if (res.data.code) {
          wx.showToast({
            title: '操作成功',
          })
          that.data.items = [];
          that.setData(that.data);
          that.setData({
            showModal: false
          })
          that.loadData();
        }
      },
      fail: function(e) {
        //util.showError("设置失败".e)
      }
    })
  },
  openzp: function(e) {
    // var workid = e.currentTarget.dataset.workid;
    // var model = e.currentTarget.dataset.model;
    this.setData({
      showModal: true,

    })
  },

  selectlogo: function(e) {
    var that = this;
    if (that.data.currentTab === e.currentTarget.dataset.index) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.index
      })
    }
  },
  bindKeyInput: function(e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);

  },
  // 点击获取手机验证码
  clickGainCode(e) {
    if (!this.data.clikType) {
      this.gainCode();
    }
  },
  //输入手机号获取验证码
  gainCode() {
    var that = this;
    if (this.data.mobile == '' || this.data.mobile == null) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
      })
    } else {
      that.setData({
        clikType: true
      })
      if (that.data.timeNum < timeOut) {} else {
        wx.request({
          url: applet_park_api_url + '/api/sendSms',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: "POST",
          data: {
            phoneNumber: this.data.mobile
          },
          success: function(res) {
            if (res.data.code == 0) {
              that.getTime();
              if (res.data.success) {} else {}
            } else {
              wx.showToast({
                title: res.data.message,
                icon: 'none',
              })
            }

          }
        })
      }
    }
  },
  //获取验证码时候显示时间
  getTime() {
    var that = this;
    var timer = setInterval(function() {
      that.data.timeNum--;
      if (that.data.timeNum <= 0) {
        that.setData({
          tipsCode: "重新获取验证码",
          timeNum: timeOut,
          clikType: false
        });
        clearInterval(timer);
      } else {
        that.setData({
          tipsCode: "发送" + that.data.timeNum + "秒",
        })
      }
    }, 1000)
  },

  // 设置并登录
  submit: function() {
    if (!this.data.mobile) {
      util.showModal('请填写手机号');
      return;
    }
    if (!this.data.authCode) {
      util.showModal('请填写验证码');
      return;
    }
    var openId = wx.getStorageSync("OPENID");
    var userInfo = wx.getStorageSync("USER_INFO");
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/sysUsers/validatePhone',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'PUT',
      data: {
        phoneNumber: that.data.mobile,
        verificationCode: that.data.authCode,
        wxOpenId: openId,
        nickName: userInfo.nickName
      },
      success: function(res) {
        if (res.data.code == 0) {

          
          var userInfo = wx.getStorageSync("USER_INFO");
          
          userInfo.bindPhone = res.data.data.mobile;
          userInfo.nickName = res.data.data.nickName;
          wx.setStorageSync("USER_INFO", userInfo);
          wx.setStorageSync("nickName", res.data.data.nickName);
          wx.setStorageSync("id", res.data.data.id);
          wx.setStorageSync("userRoles", res.data.data.userRoles);
          
          wx.showToast({
            title: '登录成功',
            icon: 'success',
          })
          // wx.switchTab({
          //   url: '../index/index',
          // })

          wx.redirectTo({
            url: '../index/index',
          })
        } else {
          util.showModal(res.data.message);
        }
      },
      fail: function(res) {
        util.showModal('服务器开小差,请稍后重试');
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})